package com.zhangwoo.spider.client.analyse;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.meiya.common.FileUtil;
import com.meiya.common.XmlUtil;
import com.meiya.common.string.StringUtil;
import com.zhangwoo.spider.po.Conversation;

public class XmlAnalyser {

	public static void main(String[] args) {
		String html=FileUtil.readFile("d:\\t","utf-8");

		XPath xpath = XPathFactory.newInstance().newXPath();
		Document docHtml = null;
		try {
			docHtml = XmlUtil.formatToDoc(html);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			NodeList cons = (NodeList) xpath.evaluate("//DIV[@class='text clearfix']", docHtml,XPathConstants.NODESET);
			for (int i = 0; i < cons.getLength(); i++) {
				Conversation result = new Conversation();
				result.setContent(xpath.evaluate("DIV[@class='title_top']/text()", cons.item(i))+xpath.evaluate("DIV[@class='s_cont']/text()", cons.item(i)));
				result.setSelfLink(xpath.evaluate("DIV[@class='title_top']/A/@href", cons.item(i)));
				result.setPublishTime(StringUtil.match(xpath.evaluate("DIV[@class='title clearfix']/SPAN[@class='level']/text()", cons.item(i)),"\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")[0]);
			}
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
